package com.tangxz.generate_code.pojo;

import com.tangxz.generate_code.util.DataBaseUtil;
import com.tangxz.generate_code.util.ResourceUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;


@Slf4j
public class DataBaseVo {
    private String database;
    private String diver_name;
    private String database_url;
    private String database_host;
    private String database_port;
    private String database_username;
    private String database_password;
    private String database_name;
    private List<Map<String, Object>> tableInfos;

    //直接传url，废弃
    /**
     * @param database          mysql
     * @param diver_name        com.mysql.jdbc.Driver
     * @param database_url      jdbc:mysql://localhost:3306/jeecg-boot
     * @param database_username root
     * @param database_password root
     */
    public DataBaseVo(String database, String diver_name, String database_url, String database_username, String database_password, String database_name) {
        this.database = database;
        this.diver_name = diver_name;
        this.database_url = database_url;
        this.database_username = database_username;
        this.database_password = database_password;
        this.database_name = database_name;
    }

    /**
     * @param database          mysql
     * @param diver_name        com.mysql.jdbc.Driver
     * @param database_host     localhost
     * @param database_port     3306
     * @param database_username root
     * @param database_password root
     * @param database_name     jeecg-boot
     */
    public DataBaseVo(String database, String diver_name, String database_host, String database_port, String database_username, String database_password, String database_name) {
        this.database = database;
        this.diver_name = diver_name;
        this.database_host = database_host;
        this.database_port = database_port;
        this.database_username = database_username;
        this.database_password = database_password;
        this.database_name = database_name;
    }

    /**
     * 通过路径解析配置文件
     *
     * @param databasePath 配置文件相对路径或者全路径，相对路径是相对于resource的相对路径
     */
    public DataBaseVo(String databasePath) {
        ResourceBundle databaseResource = ResourceUtil.loadResource(databasePath);
        if (databaseResource == null) {
            log.debug("通过class目录加载配置文件 " + databasePath);
            databaseResource = ResourceBundle.getBundle(databasePath);
        }
        //注入数据库配置
        database = databaseResource.getString("database");
        diver_name = databaseResource.getString("diver_name");
        database_url = databaseResource.getString("database_url");
        database_host = databaseResource.getString("database_host");
        database_port = databaseResource.getString("database_port");
        database_username = databaseResource.getString("database_username");
        database_password = databaseResource.getString("database_password");
        database_name = databaseResource.getString("database_name");
    }


    public String getDatabase() {
        return database;
    }

    public String getDiver_name() {
        return diver_name;
    }

    public String getDatabase_url() {
        if (database_url == null || database_url.isEmpty()) {
//            jdbc:mysql://localhost:3306/test
            this.database_url = "jdbc:" + database + "://" + database_host + ":" + database_port;
        }
        log.info("database_url="+ DataBaseUtil.getDatabaseUrl(database_url));
        return DataBaseUtil.getDatabaseUrl(database_url);
    }

    public String getDatabase_host() {
        return database_host;
    }

    public String getDatabase_port() {
        return database_port;
    }

    public String getDatabase_username() {
        return database_username;
    }

    public String getDatabase_password() {
        return database_password;
    }

    public String getDatabase_name() {
        return database_name;
    }

    public List<Map<String, Object>> getTableInfos() {
        return tableInfos;
    }

    public void setTableInfos(List<Map<String, Object>> tableInfos) {
        this.tableInfos = tableInfos;
    }

    public void setDatabase_name(String database_name) {
        this.database_name = database_name;
    }

    @Override
    public String toString() {
        return "DataBaseVo{" +
                "database='" + database + '\'' +
                ", diver_name='" + diver_name + '\'' +
                ", database_url='" + database_url + '\'' +
                ", database_host='" + database_host + '\'' +
                ", database_port='" + database_port + '\'' +
                ", database_username='" + database_username + '\'' +
                ", database_password='" + database_password + '\'' +
                ", database_name='" + database_name + '\'' +
                '}';
    }
}
